﻿/****************************** Module Header ******************************\
 * Module Name:  ProgramUACHelper.cs
 * Module Version:  1.0.0 (2013-7-11)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is use for run a mutex program. 
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UCF.Core.Forms
{
    /// <summary>
    /// Win7 UAC账户控制助手类 [2013-7-11 by sutroon]
    /// </summary>
    public class ProgramUACHelper
    {
        public static bool CheckUAC()
        {
            System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
            Application.EnableVisualStyles();

            System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
            //判断当前登录用户是否为管理员  
            return principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
        }
        public static void RunWithUAC(params string[] args)
        {
            //创建启动对象  
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            //设置运行文件  
            startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
            //设置启动参数  
            if (args.Length > 0) startInfo.Arguments = String.Join(" ", args);
            //设置启动动作,确保以管理员身份运行  
            startInfo.Verb = "runas";
            //如果不是管理员，则启动UAC  
            System.Diagnostics.Process.Start(startInfo);
            //退出  
            System.Windows.Forms.Application.Exit();
        }
    }
}
